﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKTX1._0.DAO;
using QLKTX1._0.DTO;
using System.Globalization;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;


namespace QLKTX1._0.GUI.QLSV
{
    public partial class FrmQLHSSV : Form
    {
        private TreeNode root;
       
        public FrmQLHSSV()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();          

        }

        private void seachByOption(string str,int type)
        {
            tvdssv.Nodes.Clear();
            root = tvdssv.Nodes.Add("Danh Sách Sinh Viên"); // Not goc
            root.Tag = 0;
            int f = 0;
            int sum = 0;
            if (type == 1) // tim kiem theo masv
            {
                foreach (SinhVienDTO value in SinhVienDAO.dsSinhVien().Where(i => i.MaSinhVien.Equals(str)))
                {
                    TreeNode c = new TreeNode();
                    c.Text = value.HoVaTen; // hien thi ten khu
                    c.Tag = value.MaSoTheNoiTru; // lay ma
                    tvdssv.Nodes[0].Nodes.Add(c);//hoi
                    sum++;
                    // -- xu ly chon node dau tieu
                    if (f == 0)
                    {
                        tvdssv.SelectedNode = c;
                    }
                    tvdssv.ExpandAll();//hoi
                    f++;
                }
            }
            else if (type==2) // tim kiem theo ho ten
            {
                foreach (SinhVienDTO value in SinhVienDAO.dsSinhVien().Where(i=>i.HoVaTen.ToLower().Contains(str.ToLower())))
                {
                    TreeNode c = new TreeNode();
                    c.Text = value.HoVaTen; // hien thi ten khu
                    c.Tag = value.MaSoTheNoiTru; // lay ma
                    tvdssv.Nodes[0].Nodes.Add(c);//hoi
                    sum++;
                    // -- xu ly chon node dau tieu
                    if (f == 0)
                    {
                        tvdssv.SelectedNode = c;
                    }
                    tvdssv.ExpandAll();//hoi
                    f++;
                }
            }
            else if (type == 3) // tim kiem theo ngay sinh
            {
                foreach (SinhVienDTO value in SinhVienDAO.dsSinhVien().Where(i => i.NgaySinh.ToString("dd/MM/yyyy").ToLower().Contains(str.ToLower())))
                {
                    TreeNode c = new TreeNode();
                    c.Text = value.HoVaTen; // hien thi ten khu
                    c.Tag = value.MaSoTheNoiTru; // lay ma
                    tvdssv.Nodes[0].Nodes.Add(c);//hoi
                    sum++;
                    // -- xu ly chon node dau tieu
                    if (f == 0)
                    {
                        tvdssv.SelectedNode = c;
                    }
                    tvdssv.ExpandAll();//hoi
                    f++;
                }
            }
            else // tim kiem theo cmnd
            {
                foreach (SinhVienDTO value in SinhVienDAO.dsSinhVien().Where(i => i.CMND.ToLower().Equals(str.ToLower())))
                {
                    TreeNode c = new TreeNode();
                    c.Text = value.HoVaTen; // hien thi ten khu
                    c.Tag = value.MaSoTheNoiTru; // lay ma
                    tvdssv.Nodes[0].Nodes.Add(c);//hoi
                    sum++;
                    // -- xu ly chon node dau tieu
                    if (f == 0)
                    {
                        tvdssv.SelectedNode = c;
                    }
                    tvdssv.ExpandAll();//hoi
                    f++;
                }
            }

            lbSumSV.Text = "Số sinh viên được tìm thấy :" + sum.ToString();
            
        }

        private void showTree(int filter)
        {
            tvdssv.Nodes.Clear();

            root = tvdssv.Nodes.Add("Danh Sách Sinh Viên"); // Not goc

            root.Tag = 0;

            int f = 0;

            int sum = 0;

            if (filter == 0)
            {

                foreach (SinhVienDTO value in SinhVienDAO.dsSinhVien())
                {
                    TreeNode c = new TreeNode();
                    c.Text = value.HoVaTen; // hien thi ten khu
                    c.Tag = value.MaSoTheNoiTru; // lay ma
                    tvdssv.Nodes[0].Nodes.Add(c);//hoi
                    sum++;
                    // -- xu ly chon node dau tieu
                    if (f == 0)
                    {
                        tvdssv.SelectedNode = c;
                    }
                    tvdssv.ExpandAll();//hoi
                    f++;

                }
            }
            else
            {
                foreach (SinhVienDTO value in SinhVienDAO.dsSinhVien().Where(i=>i.MaNamHoc.MaNamHoc==filter))
                {
                    TreeNode c = new TreeNode();
                    c.Text = value.HoVaTen; // hien thi ten khu
                    c.Tag = value.MaSoTheNoiTru; // lay ma
                    tvdssv.Nodes[0].Nodes.Add(c);//hoi
                    sum++;
                    // -- xu ly chon node dau tieu
                    if (f == 0)
                    {
                        tvdssv.SelectedNode = c;
                    }
                    tvdssv.ExpandAll();//hoi
                    f++;

                }
            }
            lbSumSV.Text = "Số sinh viên được tìm thấy :"+sum.ToString();
        }
        private void FrmQLHSSV_Load(object sender, EventArgs e)
        {         
            anhSV.Height = 152;
            anhSV.Width = 102;
            btnLuu.Enabled = false;     
           this.showTree(0);
         
          
            //----------Hien thi ten LOP len Combobox--------------- 
            cmbLop.DataSource = LopDAO.dsLop();
            cmbLop.DisplayMember = "tenLop";//doi tuong hien thi tren combobox
            cmbLop.ValueMember = "maLop";// 
            //----------Hien thi ten Khoa Combobox--------------- 
            cmbKhoa.DataSource = KhoaDAO.dsKhoa();
            cmbKhoa.DisplayMember = "tenKhoa";
            cmbKhoa.ValueMember = "maKhoa";
            //----------Hien thi ten Dan Toc Combobox--------------- 
            cmbDanToc.DataSource = DanTocDAO.dsDanToc();
            cmbDanToc.DisplayMember = "tenDanToc";
            cmbDanToc.ValueMember = "maDanToc";
            //----------Hien thi ten Ton Giao Combobox--------------- 
            cmbTonGiao.DataSource = TonGiaoDAO.dsTonGiao();
            cmbTonGiao.DisplayMember = "tenTonGiao";
            cmbTonGiao.ValueMember = "maTonGiao";
            //----------Hien thi ten Hoc Ky Combobox--------------- 
            //cmbHocKy.DataSource = HocKyDAO.dsHocKy();
            //cmbHocKy.DisplayMember = "tenHocKy";
            //cmbHocKy.ValueMember = "maHocky";
            //----------Hien thi ten Nam Hoc Combobox--------------- 
            cmbNam.DataSource = NamHocDAO.dsNamHoc();
            cmbNam.DisplayMember = "tenNamHoc";
            cmbNam.ValueMember = "maNamHoc";
            //----------Hien thi ten QuanHe Combobox---------------
            cmbQuanHe.DataSource = QuanHeDAO.dsQuanHe();
            cmbQuanHe.DisplayMember = "tenQuanHe";
            cmbQuanHe.ValueMember = "maQuanHe";
            //---------------------------------//
            cmbXemTheoNH.DataSource = NamHocDAO.filter();
            cmbXemTheoNH.DisplayMember = "tenNamHoc";
            cmbXemTheoNH.ValueMember = "maNamHoc";
           
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text.Equals("Thêm"))
            {
                
                btnThem.Enabled = true;
                btnCapNhat.Enabled = false;
                btnXoa.Enabled = false;
                btnLuu.Enabled = true;          
                anhSV.Image = null;
                grNhanThan.Enabled = true;
                grThongTinSV.Enabled = true;
                txtNgayDK.Text = DateTime.Now.ToString("dd/MM/yyyy");
              
                foreach (Control x in this.Controls)
                {
                    if (x is TabControl)
                    {
                        foreach (Control t in x.Controls)
                        {
                            if (t is TabPage)
                            {
                                foreach (Control t2 in t.Controls)
                                {
                                    if (t2 is GroupBox)
                                    {
                                        foreach (Control t3 in t2.Controls)
                                        {
                                            if (t3 is TextBox)
                                            {
                                                if (!t3.Name.Equals("txtNgaySinh") && !t3.Name.Equals("txtNgayDK") && !t3.Name.Equals("txtSoTheNoiTru"))
                                                {
                                                    t3.Text = "";
                                                }
                                            }
                                        }
                                    }                                    
                                }
                            }
                        }
                    }
                    
                }
               
                txtSoTheNoiTru.Text = Service.generateNoAuto("SinhVien", "maSoTheNoiTru", "ID", 100000);
                btnThem.Text = "Hủy bỏ";               
                anhSV.Image = Image.FromFile(Service.saveAndGetFileFromApp("FILES", "noimage.jpg"));
            }
            else
            {
                btnThem.Text = "Thêm";
                btnThem.Enabled = true;
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = true;
                btnLuu.Enabled = false;

                foreach (Control ctrl in grNhanThan.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        ((TextBox)ctrl).ReadOnly = true;
                    }
                }

                foreach (Control ctrl in grThongTinSV.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        ((TextBox)ctrl).ReadOnly = true;
                    }
                }

              //  grNhanThan.;
             //   grThongTinSV.Enabled = false;
                //this.xuLyTabLop(true, true, true, false, false, false);
                //this.FrmQLSV_Load(sender, e);
                this.showTree(0);
            } 
            

          
        }

        private void tvdssv_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //MessageBox.Show(e.Node.Text);
            //txtHoTenNT.Text = e.Node.Text;
            String id = e.Node.Tag.ToString();

            if (!id.Equals("0"))
            {
                SinhVienDTO sv = SinhVienDAO.getInfoSinhVien(id);
                txtSoTheNoiTru.Text = sv.MaSoTheNoiTru;
                txtHoTenSV.Text = sv.HoVaTen;
                txtMaSV.Text = sv.MaSinhVien;
                txtCMND.Text = sv.CMND;
                txtDienThoaiSV.Text = sv.DienThoai;
                if (sv.GioiTinh == 1)
                {
                    optNam.Checked = true;
                }
                else optNu.Checked = true;               

                if (!sv.HinhAnh.Equals(""))
                {                    
                    anhSV.Image = Image.FromFile(Service.saveAndGetFileFromApp("FILES",sv.HinhAnh)); // gan vao box
                    anhSV.Height = 152;
                    anhSV.Width = 102;

                }
                else anhSV.Image = Image.FromFile(Service.saveAndGetFileFromApp("FILES", "noimage.jpg"));

                    txtQueQuan.Text = sv.QueQuan;

                    cmbDanToc.Text = sv.MaDanToc.TenDanToc;
                    cmbLop.Text = sv.MaLop.TenLop;
                    cmbKhoa.Text = sv.MaKhoa.TenKhoa;
                    cmbTonGiao.Text = sv.MaTonGiao.TenTonGiao;
                    cmbNam.Text = sv.MaNamHoc.TenNamHoc;
                    txtNgaySinh.Text = sv.NgaySinh.ToString("dd/MM/yyyy");
                    txtNgayDK.Text = sv.NgayDangKyKTX.ToString("dd/MM/yyyy");

                    txtHoTenNT.Text = sv.TenNhanThan;
                    txtDiaChiNT.Text = sv.DiaChiNhanThan;
                    txtDienThoaiNT.Text = sv.DienThoaiNhanThan;
                    txtNgheNghiep.Text = sv.NgheNghiep;
                    cmbQuanHe.Text = sv.MaQuanHe.TenQuanHe;
                    txtIndex.Text = e.Node.Index.ToString();

                    //----------txtNgayDK An-------------
                    txtNgayDK.Enabled = false;
                    txtNgayDK.ForeColor = Color.Red;
                    //MessageBox.Show(e.Node.Index.ToString());
                /*
                    foreach (Control ctrl in grNhanThan.Controls)
                    {
                        if (ctrl is TextBox)
                        {
                            ((TextBox)ctrl).ReadOnly = true;
                        }
                    }

                    foreach (Control ctrl in grThongTinSV.Controls)
                    {
                        if (ctrl is TextBox)
                        {
                            ((TextBox)ctrl).ReadOnly = true;
                        }
                    }
                   */
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (btnCapNhat.Text.Equals("Cập Nhật"))
            {
                tvdssv.Enabled = false;
                btnThem.Enabled = false;
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = false;
                btnLuu.Enabled = true;
                // txtSoTheNoiTru.Text = "";
                // txtHoTenSV.Text = "";
                // txtMaSV.Text = "";
                // txtDienThoaiSV.Text = "";
                // txtQueQuan.Text = "";
                // txtNgaySinh.Text = "";
                // txtCMND.Text = "";
                //  btnThem.Text = "Hủy bỏ";
                // reset cac thuoc tinh
                //anhSV.Image = null;
                grNhanThan.Enabled = true;
                grThongTinSV.Enabled = true;
                //this.xuLyTabLop(false, true, false, true, false, true);

                btnCapNhat.Text = "Hủy bỏ";
            }
            else
            {
                tvdssv.Enabled = true;
                btnCapNhat.Text = "Cập Nhật";
                btnThem.Enabled = true ;
                btnCapNhat.Enabled = true;
                btnXoa.Enabled = true ;
                btnLuu.Enabled = false;
                this.showTree(0);
            }
        }

        private void txtNgaySinh_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            ofdAnhSV.FileName = "";
            ofdAnhSV.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";

            if (ofdAnhSV.ShowDialog() == DialogResult.OK)
            {     

                anhSV.Image = Image.FromFile(ofdAnhSV.FileName);

                anhSV.Height = 152;

                anhSV.Width = 102;                
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (btnThem.Text.Equals("Hủy bỏ"))
            {
                if (txtSoTheNoiTru.Text.Equals("") || txtMaSV.Text.Equals("") || txtHoTenSV.Text.Equals("") || txtNgaySinh.Text.Equals("") || txtDienThoaiSV.Text.Equals("") || txtQueQuan.Text.Equals("") || txtCMND.Text.Equals("") || txtHoTenNT.Text.Equals("") || txtDiaChiNT.Text.Equals("") || txtDienThoaiNT.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    String Id = txtSoTheNoiTru.Text;
                    String hoten = txtHoTenSV.Text;
                    String dob = txtNgaySinh.Text;
                    String tel = txtDienThoaiSV.Text;
                    String qq = txtQueQuan.Text;
                    String cmnd = txtCMND.Text;
                    String masv = txtMaSV.Text;

                    int gt = 1;

                    if (optNu.Checked)
                    {
                        gt = 0;
                    }

                    // thong tin nhan than
                    String hoTenNT = txtHoTenNT.Text;
                    String qQNT = txtDiaChiNT.Text;
                    String telNT = txtDienThoaiNT.Text;
                    string nn = txtNgheNghiep.Text;
                    


                    SinhVienDTO sv = new SinhVienDTO();
                    sv.MaSoTheNoiTru = Id;
                    sv.HoVaTen = hoten;
                    sv.NgaySinh = DateTime.Now;
                    sv.CMND = cmnd;
                    sv.DienThoai = tel;
                    sv.QueQuan = qq;
                    sv.MaSinhVien = masv;
                    sv.GioiTinh = gt;
                    sv.TenNhanThan = hoTenNT;
                    sv.DienThoaiNhanThan = telNT;
                    sv.DiaChiNhanThan = qQNT;
                    sv.NgheNghiep = nn;
                    sv.NgayDangKyKTX = DateTime.Now;
                    sv.NgaySinh = DateTime.ParseExact(txtNgaySinh.Text, "dd/MM/yyyy", null);

                    int lopsh = Int32.Parse(cmbLop.SelectedValue.ToString());
                    int dantoc = Int32.Parse(cmbDanToc.SelectedValue.ToString());
                    int tongiao = Int32.Parse(cmbTonGiao.SelectedValue.ToString());
                    int khoa = Int32.Parse(cmbKhoa.SelectedValue.ToString());
                    int qh = Int32.Parse(cmbQuanHe.SelectedValue.ToString());
                    int nh = Int32.Parse(cmbNam.SelectedValue.ToString());

                    LopDTO olop = new LopDTO();
                    olop.MaLop = lopsh;
                    sv.MaLop = olop;

                    DanTocDTO odt = new DanTocDTO();
                    odt.MaDanToc = dantoc;
                    sv.MaDanToc = odt;

                    TonGiaoDTO otg = new TonGiaoDTO();
                    otg.MaTonGiao = tongiao;
                    sv.MaTonGiao = otg;

                    KhoaDTO okhoa = new KhoaDTO();
                    okhoa.MaKhoa = khoa;
                    sv.MaKhoa = okhoa;

                    QuanHeDTO oqh = new QuanHeDTO();
                    oqh.MaQuanHe = qh;
                    sv.MaQuanHe = oqh;

                    NamHocDTO onh = new NamHocDTO();
                    onh.MaNamHoc = nh;

                    sv.MaNamHoc = onh;
                  

                    // lay hinh anh tu box

                    if (anhSV.Image != null&&!ofdAnhSV.FileName.Equals("-1"))
                    {

                        Image image = Image.FromFile(ofdAnhSV.FileName); // Lay hinh anh tu dialog

                        string filename = Path.GetFileName(ofdAnhSV.FileName); // lay duong dan

                        //string ext = Path.GetExtension(filename);

                        string strtemp = System.DateTime.Now.ToString("ddMMyyhhmmss_") + filename; // tao ten file moi

                        //lbAnhSV.Text = strtemp;

                        //string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin")); // noi luu

                        image.Save(Service.saveAndGetFileFromApp("FILES",strtemp));//path + "\\FILES\\" + strtemp); // thuc hien luu xuong thu muc

                        sv.HinhAnh = strtemp; //lay ten file hinh anh sv
                    }
                    else sv.HinhAnh = "";

                    // kiem tra cmnd
                    if (ValidateData.checkDataExistabled("sinhvien", "cmnd", cmnd))
                    {
                        
                        MessageBox.Show("Số chứng minh nhân dân : "+cmnd+" đả tồn tại", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtCMND.ForeColor = Color.Red;
                        txtCMND.Focus();
                    } else if (ValidateData.checkDataExistabled("sinhvien","masinhvien",masv)){
                        MessageBox.Show("Mã sinh viên : "+masv+" đả tồn tại", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtMaSV.ForeColor = Color.Red;
                        txtMaSV.Focus();
                    } else {

                        SinhVienDAO.themSinhVien(sv);

                        //MessageBox.Show(DateTime.Now.ToShortDateString());


                        TreeNode c = new TreeNode();
                        c.Text = txtHoTenSV.Text;
                        c.Tag = txtSoTheNoiTru.Text;
                        tvdssv.Nodes[0].Nodes.Add(c);

                        btnThem.Text = "Thêm";
                        btnThem.Enabled = true;
                        btnCapNhat.Enabled = true;
                        btnXoa.Enabled = true;
                        btnLuu.Enabled = false;
                    }
                    
                    //this.FrmQLHSSV_Load(sender, e);

                }
            }
            else if (btnCapNhat.Text.Equals("Hủy bỏ"))
            {
                if (txtSoTheNoiTru.Text.Equals("") || txtMaSV.Text.Equals("") || txtHoTenSV.Text.Equals("") || txtNgaySinh.Text.Equals("") || txtDienThoaiSV.Text.Equals("") || txtQueQuan.Text.Equals("") || txtCMND.Text.Equals("") || txtHoTenNT.Text.Equals("") || txtDiaChiNT.Text.Equals("") || txtDienThoaiNT.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    String Id = txtSoTheNoiTru.Text;
                    String hoten = txtHoTenSV.Text;
                    String dob = txtNgaySinh.Text;
                    String tel = txtDienThoaiSV.Text;
                    String qq = txtQueQuan.Text;
                    String cmnd = txtCMND.Text;
                    String masv = txtMaSV.Text;

                    int gt = 1;

                    if (optNu.Checked)
                    {
                        gt = 0;
                    }

                    // thong tin nhan than
                    String hoTenNT = txtHoTenNT.Text;
                    String qQNT = txtDiaChiNT.Text;
                    String telNT = txtDienThoaiNT.Text;
                    string nn = txtNgheNghiep.Text;



                    SinhVienDTO sv = SinhVienDAO.getInfoSinhVien(Id);
                    sv.MaSoTheNoiTru = Id;
                    sv.HoVaTen = hoten;
                    sv.NgaySinh = DateTime.Now;
                    sv.CMND = cmnd;
                    sv.DienThoai = tel;
                    sv.QueQuan = qq;
                    sv.MaSinhVien = masv;
                    sv.GioiTinh = gt;
                    sv.TenNhanThan = hoTenNT;
                    sv.DienThoaiNhanThan = telNT;
                    sv.DiaChiNhanThan = qQNT;
                    sv.NgheNghiep = nn;
                    sv.NgayDangKyKTX = DateTime.Now;
                    sv.NgaySinh = DateTime.ParseExact(txtNgaySinh.Text, "dd/MM/yyyy", null);

                    int lopsh = Int32.Parse(cmbLop.SelectedValue.ToString());
                    int dantoc = Int32.Parse(cmbDanToc.SelectedValue.ToString());
                    int tongiao = Int32.Parse(cmbTonGiao.SelectedValue.ToString());
                    int khoa = Int32.Parse(cmbKhoa.SelectedValue.ToString());
                    int qh = Int32.Parse(cmbQuanHe.SelectedValue.ToString());
                    int nh = Int32.Parse(cmbNam.SelectedValue.ToString());

                    LopDTO olop = new LopDTO();
                    olop.MaLop = lopsh;
                    sv.MaLop = olop;

                    DanTocDTO odt = new DanTocDTO();
                    odt.MaDanToc = dantoc;
                    sv.MaDanToc = odt;

                    TonGiaoDTO otg = new TonGiaoDTO();
                    otg.MaTonGiao = tongiao;
                    sv.MaTonGiao = otg;

                    KhoaDTO okhoa = new KhoaDTO();
                    okhoa.MaKhoa = khoa;
                    sv.MaKhoa = okhoa;

                    QuanHeDTO oqh = new QuanHeDTO();
                    oqh.MaQuanHe = qh;
                    sv.MaQuanHe = oqh;

                    NamHocDTO onh = new NamHocDTO();
                    onh.MaNamHoc = nh;
                    sv.MaNamHoc = onh;

                    // lay hinh anh tu box

                    if (anhSV.Image != null)
                    {

                        if (!ofdAnhSV.FileName.Equals("-1") && !ofdAnhSV.FileName.Equals("")) // neu chon hinh anh moi
                        {
                            Image image = Image.FromFile(ofdAnhSV.FileName); // Lay hinh anh tu dialog

                            string filename = Path.GetFileName(ofdAnhSV.FileName); // lay duong dan

                            //string ext = Path.GetExtension(filename);

                            string strtemp = System.DateTime.Now.ToString("ddMMyyhhmmss_") + filename; // tao ten file moi

                            //lbAnhSV.Text = strtemp;

                          //  string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin")); // noi luu

                            image.Save(Service.saveAndGetFileFromApp("FILES",strtemp));//path + "\\FILES\\" + strtemp); // thuc hien luu xuong thu muc

                            sv.HinhAnh = strtemp; //lay ten file hinh anh sv
                        }                        
                    }                 



                    SinhVienDAO.capNhatSinhVien(sv);                
                    // huy bo
                    btnCapNhat.Text = "Cập Nhật";
                    btnThem.Enabled = true;
                    btnCapNhat.Enabled = true;
                    btnXoa.Enabled = true;
                    btnLuu.Enabled = false;        

                    this.showTree(0);

                    tvdssv.Enabled = true;
               
                }
            }

            
            // MessageBox.Show();
        }

        private void txtSoTheNoiTru_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtHoTenSV_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                String id = txtSoTheNoiTru.Text;
                SinhVienDTO sv = new SinhVienDTO();
                sv.MaSoTheNoiTru = id;
                SinhVienDAO.xoaSinhVien(sv);
                tvdssv.Nodes[0].Nodes.RemoveAt(Int32.Parse(txtIndex.Text));
            }
           
        }

        private void lbAnhSV_Click(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void cblXemTheoNH_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int ma = Int32.Parse(cmbXemTheoNH.SelectedValue.ToString());
           // this.showTree(ma);
        }

        private void cmbXemTheoNH_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //MessageBox.Show(cmbXemTheoNH.SelectedValue.ToString());
           // int ma = Int32.Parse();
            this.showTree(Int32.Parse(cmbXemTheoNH.SelectedValue.ToString()));
        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {
            FrmTimKiem f = new FrmTimKiem();

            if (f.ShowDialog(this) == DialogResult.OK)
            {
               // MessageBox.Show(f.MaSV + "/" + f.HoTen + "/" + f.CMND + "/" + f.DOB);
                
                if (!f.MaSV.Equals(""))
                {
                    this.seachByOption(f.MaSV, 1);
                }
                else if (!f.HoTen.Equals(""))
                {
                    this.seachByOption(f.HoTen, 2);
                }
                else if (!f.DOB.Equals(DateTime.Now.ToString("dd/MM/yyyy")))
                {
                    this.seachByOption(f.DOB, 3);
                }
                else
                {
                    this.seachByOption(f.CMND, 4);
                }
                

            }

            f.Dispose();
            
           
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void txtCMND_MouseLeave(object sender, EventArgs e)
        {

        }

        private void txtCMND_Validated(object sender, EventArgs e)
        {
            if (txtCMND.TextLength < 9 || txtCMND.TextLength >12)
            {
                //MessageBox.Show("Số CMND từ 9 đến 12 chữ số");

                FrmThongBao f = new FrmThongBao("Số CMND từ 9 đến 12 chữ số");

                f.StartPosition = FormStartPosition.CenterScreen;

                f.ShowDialog();

                txtCMND.ForeColor = Color.Red;

                txtCMND.Focus();
            }
            else if (ValidateData.checkDataExistabled("sinhvien", "cmnd", txtCMND.Text))
            {
              //  MessageBox.Show();

                FrmThongBao f = new FrmThongBao("Số CMND : [" + txtCMND.Text + "] Đả tồn tại");

                f.StartPosition = FormStartPosition.CenterScreen;

                f.ShowDialog();

                txtCMND.ForeColor = Color.Red;
                txtCMND.Focus();
            }
            
        }

        private void txtMaSV_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtDienThoaiSV_Validated(object sender, EventArgs e)
        {
            string sdt = txtDienThoaiSV.Text; 
          
            int isdt;

            if (!sdt.ElementAt(0).ToString().Equals("0")||!int.TryParse(sdt,out isdt))
            {
                // ko bat dau = 0 thi ko hop le

                FrmThongBao f = new FrmThongBao("Số điện thoại : [" + sdt + "] không hợp lệ !");

                f.StartPosition = FormStartPosition.CenterScreen;

                f.ShowDialog();

                txtDienThoaiSV.ForeColor = Color.Red;

                 txtDienThoaiSV.Focus();
            }
            else if (sdt.Length < 10 || sdt.Length > 12)
            {
                FrmThongBao f = new FrmThongBao("Số điện thoại : [" + sdt + "] có độ dài không hợp lệ !");

                f.StartPosition = FormStartPosition.CenterScreen;

                f.ShowDialog();

                txtDienThoaiSV.ForeColor = Color.Red;

                txtDienThoaiSV.Focus();
            }
        }
    }
}
